// index.js

import orderService from '@/apis/order'
import userService from '../../apis/user'

Page({
  data: {
    // 产值数据
    statistics: {},
    // 转换后的统计数据结构
    statGroups: [
      // {
      //   title: '今日统计',
      //   key: 'today',
      //   items: [
      //     { label: '平均价格', valueKey: 'average_price', unit: '元' },
      //     { label: '转化率', valueKey: 'conversion_rate', unit: '%' },
      //     { label: '总订单数', valueKey: 'total_count' },
      //     // ... 其他字段
      //   ]
      // },
      // 同理添加 month 和 all 的结构
    ],

    motto: 'Hello World',
    avatar_url: '',
    defaultAvatarUrl: '/assets/imgs/user_unlogin.png'
  },
  // 修改后的获取用户信息方法
  async getUserInfo() {
    try {
      const { userInfo } = await wx.getUserProfile({
        desc: '展示头像'
      });
      this.setData({
        avatar_url: userInfo.avatarUrl
      });
      userService.avatar(userInfo.avatarUrl).then(res => {
        console.log(res)
      })
      // 这里可以添加上传头像到服务器的逻辑
    } catch (err) {
      console.log('用户拒绝授权', err);
    }
  },
  onLoad() {
    const app = getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
    this.myLoad();
  },
  async myLoad() {
    const app = getApp()
    console.log(app.globalData.userInfo);
    orderService.statistics().then(res => {
      const data = res.data.data;
      this.setData({
        avatar_url: app.globalData.userInfo ? app.globalData.userInfo.avatar_url : '',
        statistics: data,
        statGroups: [
          this.createStatGroup('今日', 'today', data.today),
          this.createStatGroup('本月', 'month', data.month),
          this.createStatGroup('累计', 'all', data.all)
        ]
      })
    })
  },
  // 新增方法（在 Page 对象内）
  createStatGroup(title, key, data) {
    return {
      title,
      key,
      items: Object.keys(data).map(k => ({
        label: this.getLabelByKey(k),
        valueKey: k,
        unit: this.getUnitByKey(k),
        pre: this.getPreByKey(k),
      }))
    };
  },
  getLabelByKey(key) {
    const labels = {
      'total_price': '产值',
      'total_count': '总单量',
      'conversion_rate': '转化率',
      'total_paid_count': '成交量',
      'total_priced_count': '报价单数',
      'average_price': '客单价',
    };
    return labels[key] || key;
  },
  getUnitByKey(key) {
    return {
      'total_price': '',
      'total_count': '单',
      'conversion_rate': '%',
      'total_paid_count': '单',
      'total_priced_count': '单',
      'average_price': '',
      // ... 其他单位配置
    }[key] || '';
  },
  getPreByKey(key) {
    return {
      'total_price': '¥',
      'total_count': '',
      'conversion_rate': '',
      'total_paid_count': '',
      'total_priced_count': '',
      'average_price': '¥',
    }[key] || '';
  },
  onUnload() {
    const app = getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
  },
  showModal() {
    //跳转到开始业务页面
    wx.switchTab({
      url: '/pages/sale/sale'
    })
  },
  // 修改后的生命周期函数
  onShow() {
    const app = getApp();
    app.checkToken().then(valid => {
      console.log("index.js onShow() token验证结果: " + valid);
    });
  }
})
